#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class ConfigWin(QWidget):
    def __init__(self, parent = None):
        QWidget.__init__(self, parent)
        
        self.conf = dict()
        self.mainLayout = QVBoxLayout()
        self.setMaximumHeight(400)
        self.setFixedSize(QSize(640,400))
        
        self.projectNameText = QLineEdit()
        self.projectNameLabel = MyLabel(u'项目名字')    
        hBox = QHBoxLayout()
        hBox.addWidget(self.projectNameLabel)
        hBox.addWidget(self.projectNameText)
        self.mainLayout.addLayout(hBox)

        hBox = QHBoxLayout()
        self.projectDirText = QLineEdit('./')
        self.projectDirText.setEnabled(False)
        self.projectDirLabel = MyLabel(u'项目路径')
        self.projectDirBtn = QPushButton(u'选择项目路径')
        hBox.addWidget(self.projectDirLabel)
        hBox.addWidget(self.projectDirText)    
        hBox.addWidget(self.projectDirBtn)
        self.mainLayout.addLayout(hBox)
        
        self.startUrlText = QLineEdit()
        self.startUrlLabel = MyLabel(u'开始地址')    
        hBox = QHBoxLayout()
        hBox.addWidget(self.startUrlLabel)
        hBox.addWidget(self.startUrlText)
        self.mainLayout.addLayout(hBox)
        
        self.urlRuleText = QLineEdit()
        self.urlRuleLabel = MyLabel(u'地址规则')
        hBox = QHBoxLayout()
        hBox.addWidget(self.urlRuleLabel)    
        hBox.addWidget(self.urlRuleText)
        self.mainLayout.addLayout(hBox)
        
        hBox = QHBoxLayout()
        self.sqlFileText = QLineEdit()
        self.sqlFileText.setEnabled(False)
        self.sqlFileLabel = MyLabel(u'数据库文件')
        self.sqlFileBtn = QPushButton(u'选择数据库文件')
        hBox.addWidget(self.sqlFileLabel)
        hBox.addWidget(self.sqlFileText)    
        hBox.addWidget(self.sqlFileBtn)
        self.mainLayout.addLayout(hBox)
        
        hBox = QHBoxLayout()
        self.itemFileText = QLineEdit()
        self.itemFileText.setEnabled(False)
        self.itemFileLabel = MyLabel(u'Item模块')
        self.itemFileBtn = QPushButton(u'选择Item模块文件')
        hBox.addWidget(self.itemFileLabel)
        hBox.addWidget(self.itemFileText)    
        hBox.addWidget(self.itemFileBtn)
        self.mainLayout.addLayout(hBox)


        self.mainLayout.addStretch()
        
        hBox = QHBoxLayout()
        hBox.addStretch()
        self.finishBtn = QPushButton(u'完成配置')
        hBox.addWidget(self.finishBtn)
        self.mainLayout.addLayout(hBox)
        
        self.connect(self.sqlFileBtn, SIGNAL("clicked()"), self.selectSqlFile)
        self.connect(self.itemFileBtn, SIGNAL("clicked()"), self.selectItemFile)
        self.connect(self.finishBtn, SIGNAL("clicked()"), self.finishConfig)
        self.connect(self.projectDirBtn, SIGNAL("clicked()"), self.selectProjectDir)
        self.setLayout(self.mainLayout)
        
    def selectSqlFile(self):
        s=QFileDialog.getOpenFileName(self,"Open file dialog","/","Sql files(*.sql)")  
        self.sqlFileText.setText(str(s))
          
    def selectItemFile(self):
        s=QFileDialog.getOpenFileName(self,"Open file dialog","/","python files(*.py)")  
        self.itemFileText.setText(str(s))  
        
    def selectProjectDir(self):
        s=QFileDialog.getExistingDirectory(self)
        self.projectDirText.setText(str(s))
          
    def finishConfig(self):
        urls = self.startUrlText.text()
        self.conf['urls'] = str(urls).split(';')
        self.conf['sqlfile'] = str(self.sqlFileText.text())    
        self.conf['itemsfile'] = str(self.itemFileText.text())
        self.conf['urlrule'] = str(self.urlRuleText.text())
        self.conf['projectname'] = str(self.projectNameText.text())
        self.hide()
        self.emit(SIGNAL('hided()'))
    
    def getConfig(self):
        return self.conf
        
        
class MyLabel(QLabel):
    def __init__(self, text):
        QLabel.__init__(self, text)
        self.setMinimumSize(120, 20)
        self.setMaximumSize(120, 20)
        

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    configwin = ConfigWin()
    configwin.show()
    sys.exit(app.exec_())